import mx.utils.Delegate;
import com.live5.liveplayer.MediaPlayer;
import com.live5.liveplayer.Playlist;
import com.live5.liveplayer.Mp3Controller;
import com.live5.controls.LiveProgressBar;
import mx.transitions.Tween;
import mx.transitions.easing.Regular;

class com.live5.liveplayer.ImagePlayer extends MediaPlayer
{
	var isAnimating:Boolean;
	function ImagePlayer()
	{
		MediaType="image/jpeg";
	}
	
	function DoPlay(){
		trace("image player playing");
		super.DoPlay();
		
		var imageholder = Canvas.createEmptyMovieClip("imageHolder",this.getNextHighestDepth());
		var mcLoader:MovieClipLoader =new MovieClipLoader();
		mcLoader.loadClip(this.playlist.Current.Location,imageholder);
		loaderListener.onLoadComplete=onImageLoadComplete;
		loaderListener.onLoadProgress=onImageLoadProgress;
		loaderListener.onLoadInit =onImageLoadInit;
		tweenListener.onMotionFinished=onTweenMotionFinished;
		mcLoader.addListener(loaderListener);
	}
	// Define a listener object to use with the Tween objects.
var tweenListener:Object = new Object();
// When the user clicks on an image Flash calls onImageClipPress() because
// it's assigned to the onPress property of the image movie clips. Note that
// onImageClipPress() is called as a method of the movie clip that was clicked.
// That means that the this keyword references the movie clip.
function onImageClipPress():Void {
	
      runTweens(this, 0, 0);

}
function onImageLoadComplete(sender:Object,httpStatus:Object):Void{
	runTweens(Canvas,0,0);
}
function makeNewImageClip(x:Number, y:Number):MovieClip {

  // Retrieve the next highest depth;
  var depth:Number = this.getNextHighestDepth();

  // Make a new empty movie clip. Use "imageClip" + depth so that the movie clip
  // has a unique instance name.
  var imageClip:MovieClip = this.createEmptyMovieClip("imageClip" + depth, depth);

  // Make a new empty movie clip in imageClip for the purposes of loading
  // the image into it.
  imageClip.createEmptyMovieClip("holderClip", 1);

  // Make a new empty movie clip in imageClip for the purposes of displaying
  // the download progress.
  var progressClip:MovieClip = imageClip.createEmptyMovieClip("progressClip", 2);

  // Make a movie clip in progressClip for rendering the progress as a bar chart.
  progressClip.createEmptyMovieClip("indicatorClip", 1);

  // Make a text field to diaplay the download progress as a percent.
  progressClip.createTextField("progressText", 2, 0, -25, 100, 25);
  progressClip.progressText.border = true;
  progressClip.progressText.text = "in loading queue";

  // Draw a 100 X 10 red rectangle in indicatorClip.
  progressClip.indicatorClip.lineStyle(0, 0, 0);
  progressClip.indicatorClip.beginFill(0xFF0000, 100);
  progressClip.indicatorClip.lineTo(100, 0);
  progressClip.indicatorClip.lineTo(100, 10);
  progressClip.indicatorClip.lineTo(0, 10);
  progressClip.indicatorClip.lineTo(0, 0);
  progressClip.indicatorClip.endFill();

  // Move progressClip to -50 so it is centered in imageClip.
  progressClip._x = -50;

  imageClip._x = x;
  imageClip._y = y;

  // When the user clicks on imageClip call onImageClipPress().
  imageClip.onPress = onImageClipPress;
  return imageClip;
}
// Declare variables to store the dimensions of the images when scaled up
// and down.
var widthMin:Number;
var widthMax:Number;
var heightMin:Number;
var heightMax:Number;

function onTweenMotionFinished():Void{
	isAnimating = false;
}
	// Define a listener object for the MovieClipLoader object.
	var loaderListener:Object = new Object();
	function onImageLoadProgress(holderClip:MovieClip, loadedBytes:Number, totalBytes:Number):Void{
	  // The holderClip parameter references the movie clip into which the
	  // image is loading. The parent of that movie clip is image1Clip, image2Clip,
	  // image3Clip, or image4Clip. That movie clip contains progressClip.
	  var progressClip:MovieClip = holderClip._parent.progressClip;

	  // Determine the percent that has downloaded.
	  var percent:Number = Math.round((loadedBytes / totalBytes) * 100);

	  progressClip.indicatorClip._xscale = percent;
	  progressClip.progressText.text = percent + "% downloaded";
	 }
	function onImageLoadInit(holderClip:MovieClip):Void{
	 // Make the progressbar invisible.
	  holderClip._parent.progressClip._visible = false;

	  // Move the holderClip to the center of the parent movie clip. That
	  // way when it scales it scales from the center rather than the corner.
	  //holderClip._x = -holderClip._width/2;
	  //holderClip._y = -holderClip._height/2;

	  // Make the width of the images 150 pixels when scaled down. Then determine
	  // the corresponding height while maintaining the aspect ratio.
	  widthMin = 400;
	  heightMin = 300;

	  // Likewise, set the width to 550 when scaled up, and then determine the
	  // corresponding height.
	  widthMax = 400;
	  heightMax = 300;

	  // Scale the images from 0 to the minimum dimensions.
	  var tween1:Tween = new Tween(holderClip._parent, "_width", Regular.easeOut, 0, widthMin, 10);
	  var tween2:Tween = new Tween(holderClip._parent, "_height", Regular.easeOut, 0, heightMin, 10);
	
	}
	function runTweens(imageClip:MovieClip, x:Number, y:Number):Void {

	  // If a tween is currently running then exit the function.
	  if(isAnimating) {
		return;
	  }

	  // Set isAnimating to true so Flash knows a tween is running.
	  isAnimating = true;

	  // Set the depth of the movie clip so it's at the highest z-index.
	  //imageClip.swapDepths(topDepth);

	  // Determine the starting and ending dimensions. If the movie clip is already
	  // scaled up then the ending dimensions are the minimum dimensions. Otherwise
	  // the movie clip is scaled down, and it ought to scale to the maximum
	  // dimensions.
	  var startWidth:Number = imageClip._width;
	  var endWidth:Number = imageClip._width > widthMin ? widthMin : widthMax;
	  var startHeight:Number = imageClip._height;
	  var endHeight:Number = imageClip._height > heightMin ? heightMin : heightMax;

	  // Run tweens for the _width and _height properties.
	  var tween1:Tween = new Tween(imageClip, "_width", Regular.easeOut, startWidth, endWidth, 10);
	  var tween2:Tween = new Tween(imageClip, "_height", Regular.easeOut, startHeight, endHeight, 10);

	  // Determine the coordinates for the tween.
	  var startX:Number = imageClip._x;
	  var endX:Number = imageClip._xscale > 25 ? x : 300;
	  var startY:Number = imageClip._y;
	  var endY:Number = imageClip._xscale > 25 ? y : 200;

	  // Run tweens for the _x and _y coordinates.
	  var tween3:Tween = new Tween(imageClip, "_x", Regular.easeOut, startX, endX, 10);
	  var tween4:Tween = new Tween(imageClip, "_y", Regular.easeOut, startY, endY, 10);

	  tween4.addListener(tweenListener);
	}
}
